package com.tchintech.pay.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.EditText;

import com.tchintech.pay.R;
import com.tchintech.pay.adapter.RecycleCodeAdapter;
import com.tchintech.pay.delegate.ScanOrderDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.model.entity.PaytypeResule;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.utils.AppTool;
import com.tchintech.pay.utils.NetUtilIP;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/7 0007.
 * 主扫支付界面
 */

public class ScanOrderActivity extends ActivityPresenter<ScanOrderDelegate> implements View.OnClickListener {
    private RecycleCodeAdapter adapter;
    private List<PaytypeResule> list;
    private String resultCode = "";
    private final String URL_TransactionMode_query = "TransactionMode/query";
    private String code_url = "Code.code";

    private String URL_sys = "Order/createOrder";
    //测试
    private final String URL_order = "Order/create";

    private Handler handler;
    private Map<String, String> map;

    private String busCode = "";
    private String orderAmt = "";
    /**
     * 选中的支付方式
     */
    private String payType;

    private EditText ed_je, ed_spmc, ed_spms, ed_ddms, ed_bz;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        super.initView();
        ed_je = viewDelegate.getEdittView(R.id.ed_je);
        ed_spmc = viewDelegate.getEdittView(R.id.ed_spmc);
        ed_spms = viewDelegate.getEdittView(R.id.ed_spms);
        ed_ddms = viewDelegate.getEdittView(R.id.ed_ddms);
        ed_bz = viewDelegate.getEdittView(R.id.ed_bz);
        viewDelegate.setOnClickListener(this, R.id.bt_wx, R.id.bt_zfb, R.id.iv_back);
        ed_je = viewDelegate.getEdittView(R.id.ed_je);
        handler();
    }


    @Override
    protected void initData() {
        super.initData();
        viewDelegate.setTitle("扫一扫");
        viewDelegate.setVisible();
        list = new ArrayList<PaytypeResule>();
        resultCode = getIntent().getStringExtra("AuthCode");
        busCode = getIntent().getStringExtra("busCode");
        orderAmt = getIntent().getStringExtra("orderAmt");
        qrcode();

    }

    /**
     * 二维码支付
     */
    public void qrcode() {
        map = new HashMap<String, String>();
        map.put("userId", AppConst.getUser().getId());
        map.put("orderAmt", orderAmt);
        map.put("busCode", busCode);
        map.put("authCode", resultCode);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, code_url, map, HttpCode.CODE_PAY);
    }


    /**
     * 提交数据
     */
    private void setUrlData() {
        map = new HashMap<String, String>();
        map.put("amount", ed_je.getText().toString().trim());
        map.put("payType", payType);
        map.put("arrivalType", "T0");
        map.put("qrCodeType", "SCANCODE");
        map.put("ip", NetUtilIP.getLocalIpAddress(this));
        map.put("goodsDesc", ed_spms.getText().toString().trim());
        map.put("goodsName", ed_spmc.getText().toString().trim());
        map.put("orderDesc", ed_ddms.getText().toString().trim());
        map.put("comm", ed_bz.getText().toString().trim());
        map.put("deviceNum", AppTool.getImei(this));
        map.put("payauth", resultCode);
        httpPost(URL_sys, map, HttpCode.SAOYISAO, null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_wx:
                //微信支付
                payType = "WX";
                setUrlData();
                break;
            case R.id.bt_zfb:
                //支付宝支付
                payType = "ALI";
                setUrlData();
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }

    @Override
    protected Class<ScanOrderDelegate> getDelegateClass() {
        return ScanOrderDelegate.class;
    }


    @Override
    public void httpCallback(String resultBean, int what) {
        super.httpCallback(resultBean, what);
        Message msg = new Message();
        switch (what) {
            case HttpCode.SAOYISAO:
                System.out.println("回传的测试数据" + resultBean);
                msg.obj = resultBean;
                msg.what = HttpCode.SAOYISAO_SUCCESS;
                handler.sendMessage(msg);
                break;
        }
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.SAOYISAO_SUCCESS:
                        showMsg(msg.obj.toString());
                        break;

                }
            }
        };
    }


}
